package com.example.demo;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.example.demo.datasource.DruidDynamicDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@SpringBootApplication
@MapperScan(basePackages = {"com.example.demo.mapper"})
@EnableWebSocket
public class DemoApplication {

    @Autowired
    private Environment env;

    @Resource
    private RedisTemplate redisTemplate;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public DruidDataSource defaultDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        return druidDataSource;
    }

    @Bean
    public DruidDynamicDataSource druidDynamicDataSource(){
        DruidDynamicDataSource druidDynamicDataSource = new DruidDynamicDataSource();
        druidDynamicDataSource.setDefaultTargetDataSource(defaultDataSource());

        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("defaultDataSource", defaultDataSource());
        druidDynamicDataSource.setTargetDataSources(targetDataSources);
        return druidDynamicDataSource;
    }

    @Bean
    public MybatisPlusInterceptor optimisticLockerInnerInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    @Bean
    public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(){
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(druidDynamicDataSource());
        mybatisSqlSessionFactoryBean.setTypeAliasesPackage("com.example.demo");
        //String mapperLocations = env.getProperty("mybatis-plus.mapper-locations");
        //mybatisSqlSessionFactoryBean.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
        mybatisSqlSessionFactoryBean.setPlugins(optimisticLockerInnerInterceptor());
        return mybatisSqlSessionFactoryBean;
    }

    @Bean
    public RedisTemplate<String, Object> stringSerializerRedisTemplate() {
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        return redisTemplate;
    }
}
